# import os
# import yaml
import pymysql
from dbutils.pooled_db import PooledDB
from django.conf import settings


class DBHelper:
    def __new__(cls, env=None, data_source='mysql'):
        # filename = os.path.join(os.path.dirname(__file__), '../config', 'config.yml')
        # yamlfd = open(filename, encoding="utf-8")
        # config = yaml.load(yamlfd)

        obj = object.__new__(cls)
        # if env is None:
        #     obj._env = settings.ENV
        # else:
        #     obj._env = env

        obj._pool = PooledDB(
            creator=pymysql,
            maxconnections=6,
            mincached=2,
            maxcached=5,
            host='rm-uf6mv40e7es4895h8.mysql.rds.aliyuncs.com',
            port=3306,
            user='app',
            database='consultbrain',
            password='Cloudhis1234',
            charset='utf8',
            blocking=False,
            autocommit=True
            # maxconnections=config['dbpool']['maxactive'],
            # mincached=config['dbpool']['minidle'],
            # maxcached=config['dbpool']['maxidle'],
            # host=config[data_source][obj._env]['host'],
            # port=config[data_source][obj._env]['port'],
            # user=config[data_source][obj._env]['username'],
            # database=config[data_source][obj._env]['database'],
            # password=config[data_source][obj._env]['password'],
            # charset=config[data_source][obj._env]['charset'],
            # blocking=False,
            # autocommit=bool(config['dbpool']['autocommit'] != 0)
        )
        return obj

    def selectall(self, sql, values=()):
        connection = self._pool.connection()
        cursor = connection.cursor(pymysql.cursors.DictCursor)
        try:
            cursor.execute(sql, values)
            return cursor.fetchall()
        finally:
            connection.close()
            cursor.close()

    def selectone(self, sql, values=()):
        connection = self._pool.connection()
        cursor = connection.cursor(pymysql.cursors.DictCursor)
        try:
            cursor.execute(sql, values)
            return cursor.fetchone()
        finally:
            connection.close()
            cursor.close()

    def insert(self, sql, values=(), commit=False):
        connection = self._pool.connection()
        cursor = connection.cursor(pymysql.cursors.DictCursor)
        try:
            cursor.execute(sql, values)
            lastid = cursor.lastrowid
            if commit:
                connection.commit()
                return lastid
        except Exception as e:
            if commit:
                connection.rollback()
                raise e
        finally:
            connection.close()
            cursor.close()

    def insertmany(self, sql, values=(), commit=False):
        connection = self._pool.connection()
        cursor = connection.cursor(pymysql.cursors.DictCursor)
        try:
            cursor.executemany(sql, values)
            rowcount = cursor.rowcount
            if commit:
                connection.commit()
            return rowcount
        except Exception as e:
            if commit:
                connection.rollback()
                raise e
        finally:
            connection.close()
            cursor.close()
    def update(self, sql, values=(), commit=False):
        connection = self._pool.connection()
        cursor = connection.cursor(pymysql.cursors.DictCursor)
        try:
            cursor.execute(sql, values)
            rowcount = cursor.rowcount
            if commit:
                connection.commit()
                return rowcount
            return rowcount
        except Exception as e:
            if commit:
                connection.rollback()
                raise e
        finally:
            connection.close()
            cursor.close()
    def execute(self, sql, values=(), commit=False):
        connection = self._pool.connection()
        cursor = connection.cursor(pymysql.cursors.DictCursor)
        try:
            rowcount = cursor.rowcount
            if commit:
                connection.commit()
                return rowcount
        except Exception as e:
            if commit:
                connection.rollback()
                raise e
        finally:
            connection.close()
            cursor.close()
